Points

data(crime): Lightly cleaned Houston crime from January 2010 to August 2010 geocoded with Google Maps by Houston Police Department, City of Houston

library(ggmap)
qmplot(lon, lat, data = crime)

Subset

# only violent crimes
violent_crimes <- subset(crime,
                         offense != "auto theft" &
                           offense != "theft" &
                           offense != "burglary"
)

# rank violent crimes
violent_crimes$offense <- factor(
  violent_crimes$offense,
  levels = c("robbery", "aggravated assault", "rape", "murder")
)

# restrict to downtown
violent_crimes <- subset(violent_crimes,
                         -95.39681 <= lon & lon <= -95.34188 &
                           29.73631 <= lat & lat <=  29.78400
)

theme_set(theme_bw())

qmplot(lon, lat, data = violent_crimes, colour = offense,
       size = I(3.5), alpha = I(.6), legend = "topleft")

qmplot(lon, lat, data = violent_crimes, geom = c("point","density2d"))

qmplot(lon, lat, data = violent_crimes) + facet_wrap(~ offense)

qmplot(lon, lat, data = violent_crimes, extent = "panel") + facet_wrap(~ offense)

qmplot(lon, lat, data = violent_crimes, extent = "panel", colour = offense, darken = .4) +
  facet_wrap(~ month)

Vectors

data(seal): Vector field of seal movements This vector field was produced from the data described in Brillinger, D.R., Preisler, H.K., Ager, A.A. and Kie, J.G. “An exploratory data analysis (EDA) of the paths of moving animals”. J. Statistical Planning and Inference 122 (2004), 43-63, using the methods of Brillinger, D.R., “Learning a potential function from a trajectory”, Signal Processing Letters. December (2007).

plot trajectories

qmplot(long, lat, xend = long + delta_long,
       color = I("red"), yend = lat + delta_lat, data = seals, arrow = arrow(length=unit(0.1,"cm")),geom = "segment", zoom = 5)

some options for map theme, using maytype =

qmplot(long, lat, xend = long + delta_long, maptype = "watercolor",
       yend = lat + delta_lat, data = seals,arrow = arrow(length=unit(0.1,"cm")), geom = "segment", zoom = 6)

Subset the wind fields

s <- seq(1, 227, 8)
thinwind <- subset(wind,
                   lon %in% unique(wind$lon)[s] &
                     lat %in% unique(wind$lat)[s])

Add arrows

theme_set(theme_bw(18))

qmplot(lon, lat, data = thinwind, geom = "tile", fill = spd, alpha = spd,
       legend = "bottomleft") +
  geom_leg(aes(xend = lon + delta_lon, yend = lat + delta_lat), arrow = arrow(length=unit(0.1,"cm"))) + scale_fill_gradient2("Wind Speed\nand\nDirection",low = "green", mid = scales::muted("green"), high = "red") + scale_alpha("Wind Speed\nand\nDirection", range = c(.1, .75)) + guides(fill = guide_legend(), alpha = guide_legend())

library(ggmap)
#a simple example
murder <- subset(crime, offense == "murder")
qmplot(lon, lat, data = murder, colour = I('red'), size = I(3), darken = .3)

#The layered grammar of graphics
columbia <- "Columbia university in the city of new york"
qmap(columbia, zoom = 14)

qmap(columbia, zoom = 14, source = "osm")

## maptype arguments of get_map
set.seed(500)
df <- round(data.frame(
  x = jitter(rep(-95.36, 50), amount = .3),
  y = jitter(rep( 29.76, 50), amount = .3)
), digits = 2)
map <- get_googlemap('houston', markers = df, path = df, scale = 2)
ggmap(map, extent = 'device')

qmap(columbia, zoom = 14, source = "stamen", maptype = "watercolor")

qmap(columbia, zoom = 14, source = "stamen", maptype = "toner")

qmap(columbia, zoom = 14, maptype = 53428,source = "osm")

# mark position
test_map <- ggmap(get_googlemap(center = "columbia university in the city of new york", maptype = "roadmap",zoom = 14, size = c(600, 600), extent = normal, darken = 0))
test_map$data
##         lon      lat
## 1 -73.98982 40.78820
## 2 -73.93832 40.78820
## 3 -73.98982 40.82718
## 4 -73.93832 40.82718
bb <- c(test_map$data[1, 1], test_map$data[2, 1], test_map$data[2, 1], test_map$data[2, 2])


newyork <- get_map(location = "new york")
ggmap(newyork, extent = "normal")

#ggmap in action 

# only violent crimes
violent_crimes <- subset(crime, offense != "auto theft" & offense != "theft" & offense != "burglary")
violent_crimes$offense <- factor(violent_crimes$offense, levels = c("robbery", "aggravated assault", "rape", "murder"))
violent_crimes <- subset(violent_crimes, -95.39681 <= lon & lon <= -95.34188 & 29.73631 <= lat & lat <=  29.78400)

#analysis
theme_set(theme_bw(16))
HoustonMap <- qmap("houston", zoom = 14, color = "bw", legend = "topleft")
HoustonMap +
  geom_point(aes(x = lon, y = lat, colour = offense, size = offense),
             data = violent_crimes)

HoustonMap +
  stat_bin2d(
    aes(x = lon, y = lat, colour = offense, fill = offense),
    size = .5, bins = 30, alpha = 1/2,
    data = violent_crimes
  )

houston <- get_map("houston", zoom = 14)
HoustonMap <- ggmap(houston, extent = "device", legend = "topleft")

HoustonMap +stat_density2d(
  aes(x = lon, y = lat, fill = ..level..,  alpha = ..level..),
  size = 2, h = 4, data = violent_crimes,
  geom = "polygon"
)

houston <- get_map(location = "houston", zoom = 14, color = "bw", source = "osm")
HoustonMap <- ggmap(houston, base_layer = ggplot(aes(x = lon, y = lat), data = violent_crimes))

HoustonMap + stat_density2d(aes(x = lon, y = lat, fill = ..level.., alpha = ..level..),
                            h = 5, geom = "polygon",
                            data = violent_crimes) +
  scale_fill_gradient(low = 1, high = 2) +
  facet_wrap(~ day)

#geocode function and revgeocode function
geocode("columbia university in the city of new york", output = "more")
##         lon      lat           type     loctype
## 1 -73.96411 40.80772 subway_station approximate
##                                                 address    north    south
## 1 116 st - columbia university, new york, ny 10027, usa 40.80907 40.80637
##        east      west            point_of_interest    neighborhood
## 1 -73.96276 -73.96546 116 St - Columbia University Upper Manhattan
##   sublocality_level_1 locality administrative_area_level_2
## 1           Manhattan New York             New York County
##   administrative_area_level_1       country postal_code
## 1                    New York United States       10027
cu <- geocode("columbia university in the city of new york")
cu <- as.numeric(cu)
revgeocode(cu, output = "more")
##                                       address street_number    route
## 1 2940-2950 Broadway, New York, NY 10027, USA     2940-2950 Broadway
##      neighborhood sublocality_level_1 locality administrative_area_level_2
## 1 Upper Manhattan           Manhattan New York             New York County
##   administrative_area_level_1       country postal_code
## 1                    New York United States       10027
#mapdist function
from <- c("houston", "houston", "dallas")
to <- c("waco, texas", "san antonio", "houston")
mapdist(from, to)
##      from          to      m      km    miles seconds  minutes    hours
## 1 houston waco, texas 298543 298.543 185.5146   10247 170.7833 2.846389
## 2 houston san antonio 317168 317.168 197.0882   10365 172.7500 2.879167
## 3  dallas     houston 384652 384.652 239.0228   12384 206.4000 3.440000
distQueryCheck()
.GoogleDistQueryCount
##                  time
## 1 2016-03-04 05:41:24
## 2 2016-03-04 05:41:24
##                                                                                                                                       url
## 1                   http://maps.googleapis.com/maps/api/distancematrix/json?origins=dallas&destinations=houston&mode=driving&sensor=false
## 2 http://maps.googleapis.com/maps/api/distancematrix/json?origins=houston&destinations=waco+texas%7Csan+antonio&mode=driving&sensor=false
##   elements
## 1        1
## 2        2
##route function
legs_df <- route(
  'marrs mclean science, baylor university',
  '220 south 3rd street, waco, tx 76701',
  alternatives = TRUE
)

qmap('424 clay avenue, waco, tx', zoom = 15, maptype = 'hybrid',
     base_layer = ggplot(aes(x = startLon, y = startLat), data = legs_df)) +
  geom_leg(
    aes(x = startLon, y = startLat, xend = endLon, yend = endLat,
        colour = route),
    alpha = 3/4, size = 2, data = legs_df
  )+
  labs(x = 'Longitude', y = 'Latitude', colour = 'Route') + facet_wrap(~ route, ncol = 3) + theme(legend.position = 'top')

##plotting shape file

lat_lon <- geocode("columbia university in the city of new york, 10027")
lat_lon2 <- c(lat_lon[ , 1], lat_lon[ , 2])

test_map2 <- ggmap(
  get_googlemap(
    center = lat_lon2, maptype = "roadmap", zoom = 14, size = c(600, 600), extent = normal,
    darken = 0)
)

lat_lon3 <- geocode("MorningSide park, ny")
lat_lon_df1 <- rbind(lat_lon, lat_lon3)
lat_lon_df1 <- cbind(lat_lon_df1, data.frame(location = c("Columbia University", "MorningSide Park")))
test_map2 + geom_point(data = lat_lon_df1, aes(x = lon, y = lat, fill = location), pch = 21, color = "black")